const Service = require('egg').Service;

class GoodsService extends Service {
    // 商品查询，可以设置查询区间
    async find(query) {
        const { min, max,offset,limit } = query;
        var goods;
        var total;
        if (min == 'undefined' && max == 'undefined') {
            goods = await this.ctx.model.Goods.find({}).skip(Number(offset)).limit(Number(limit));
            total = await this.ctx.model.Goods.find({})
            total = total.length
        } else {
            goods = await this.ctx.model.Goods.find({ salePrice: { $gt: min, $lt: max } }).skip(Number(offset)).limit(Number(limit));
            total = await this.ctx.model.Goods.find({ salePrice: { $gt: min, $lt: max } })
            total = total.length
        }
        return {goods,total}
    };
    // 价格排序
    async sort(query) {
        const { price,offset,limit } = query;
        var goods;
        var total;
        goods = await this.ctx.model.Goods.find().sort({ salePrice: Number(price) }).skip(Number(offset)).limit(Number(limit));
        total = await this.ctx.model.Goods.find()
        total = total.length
        return {goods,total}
    };
}

module.exports = GoodsService;